package com.mo.modular.tv.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mo.common.TvBaseController;
import com.mo.modular.tv.view.RegistUserView;
import com.mo.tv.model.ZlUser;
import com.mo.tv.service.IZlSiteService;
import com.mo.tv.service.IZlUserService;

/**
 * tv注册控制器
 *
 * @author chenxiaodong
 * @Date 2018-10-24 17:46:44
 */
@Controller
@RequestMapping("/regist")
@Api(value = "/regist", description = "注册模块")
public class RegistController extends TvBaseController {

    
    @Resource
    private IZlUserService zlUserService;

    @Autowired
	private IZlSiteService zlSiteService;
    
//    @Autowired
//    private IMuserService mUserService;
   
	@ApiOperation(value = "tv增加注册接口", 
			notes = "注册接口，主要应用于app刚刚下载安装成功时候调用的" )
//	@ApiImplicitParams({
//		@ApiImplicitParam(name = "tvid", value = "tvid", required = true, dataType = "String", paramType = "from"),
//		@ApiImplicitParam(name = "channel", value = "渠道", required = true, dataType = "String", paramType = "from"),
//		@ApiImplicitParam(name = "guid", value = "设备唯一id", required = true, dataType = "String", paramType = "from"),
//		@ApiImplicitParam(name = "vendor", value = "厂商", required = true, dataType = "String", paramType = "from"),
//		@ApiImplicitParam(name = "model", value = "型号", required = true, dataType = "String", paramType = "from"),
//		@ApiImplicitParam(name = "sysVersion", value = "系统版本号", required = true, dataType = "String", paramType = "from"),
//		@ApiImplicitParam(name = "appVersion", value = "版本号", required = true, dataType = "String", paramType = "from")
//		})

//	  @ApiOperation(value = "Place an order for a pet", response = RegistUserView.class)
    @RequestMapping(value = "/registTVAPP", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> registTVAPP(
    	      @ApiParam(value = "registUserView placed for purchasing the pet", required = true) 
    	      @RequestBody RegistUserView registUserView){ 
//    	      @RequestBody RegistUserView registUserView) {
    	String msg = "注册成功";
    	try{
//    		RegistUserView registUserView = new RegistUserView();
    		ZlUser user = insertTVInfo(registUserView);
    		Map<String,Object> map = new HashMap<String,Object>();
    		map.put("tvid", user.getTvid());
    		return writeSuccessToClient(msg, map);
    	}catch(Exception e){
    		return writeExceptionToClient(e.getMessage());
    	}
    }
	static int getRandomNum (){
		java.util.Random random=new java.util.Random();// 定义随机类
		int result=random.nextInt(999);// 返回[0,20)集合中的整数，注意不包括20
		return result+100;              // +10后，[0,20)集合变为[10,30)集合，满足要求
	}
	/**
	 * 递归修改tvid，以防止tvid重复
	 * @param tvInfo
	 * @return
	 */
	ZlUser insertTVInfo(RegistUserView registUserView){
		ZlUser tvInfo =  new ZlUser();
		BeanUtils.copyProperties(registUserView, tvInfo);
		ZlUser user = zlUserService.findUser(tvInfo.getTvid());
		if(user!=null){//已经存在
			String signature = user.getSignature();
			String randomNum = getRandomNum()+"";
			String newTVId = registUserView.getTvid() + randomNum;
			registUserView.setSignature(signature + randomNum);
			registUserView.setTvid(newTVId);
			tvInfo = insertTVInfo(registUserView);
		}
		Long id = zlUserService.insertTVInfo(tvInfo);
		tvInfo.setId(id);
		return tvInfo;
		
	}
	 /**
     * 
     */
	@ApiOperation(value = "tv用户绑定mobile", 
			notes = "tv用户绑定mobile")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<?> add(@RequestBody Map<String,Object> tv) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	
    	try{
    		ZlUser user = zlUserService.findUser((String)tv.get("userName"));
        	if(user!=null){
//        		MUser mUser = new MUser();
//        		mUser.setTvUserId(user.getId().intValue());
//            	int i = mUserService.TvRegistMobile(mUser);
        		int i = 0;
            	if(i==0){
            		map.put("res", 0);
            		map.put("msg", "绑定失败");
            	}else{
            		map.put("res", 1);
            		map.put("msg", "绑定成功");
            	}
        	}else{
        		map.put("res", -1);
        		map.put("msg", "用户信息异常");
        	}
    		
    	}catch(Exception e){
    		e.printStackTrace();
    		map.put("res", -1);
    		map.put("msg", "绑定异常");
    	}
    	
        return ResponseEntity.ok(map);
    }
    
    /**
     * tv用户验证是否绑定mobile
     */
    @RequestMapping("/validate")
    public ResponseEntity<?> validate(@RequestBody Map<String,Object> tv) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	
    	try{
    		ZlUser user = zlUserService.findUser((String)tv.get("userName"));
        	if(user!=null){
//        		MUser muser = mUserService.findMuserByTvId(user.getId().intValue());
            	
            	
        		map.put("res", 0);
        		map.put("msg", "此用户没绑定");
            	/*if(muser==null){
            	}else{
            		map.put("res", 1);
            		map.put("msg", "此用户已绑定");
            	}*/
        	}else{
        		map.put("res", -1);
        		map.put("msg", "用户信息异常");
        	}
    		
    	}catch(Exception e){
    		e.printStackTrace();
    		map.put("res", -1);
    		map.put("msg", "查询异常");
    	}
    	
        return ResponseEntity.ok(map);
    }
}
